package com.junmo.fanbizhidemo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

import com.junmo.fanbizhidemo.R;

/*
 * 项目名:    QQZoneAdvertising
 * 包名       com.azhon.advertising
 * 文件名:    OverlappingImageView
 * 创建者:    ZSY
 * 创建时间:  2017/7/14 on 13:45
 * 描述:     TODO 根据滑动距离来切换广告图
 */
public class OverlappingImageView extends View {

    private Paint paint;
    private int screenHeight;
    private Canvas frontCanvas;
    private int height;
    private int width;
    private float r = 0;
    /*绘制的矩形区域*/
    private RectF rectF;
    /*背景广告*/
    private Bitmap background;
    /*前景广告*/
    private Bitmap frontImage;
    /*前景画布*/
    private Bitmap frontBg;
    private float offsetX = 100;
    private float offsetY = 100;
    private int[] images = {R.drawable.feng, R.drawable.longg};

    public OverlappingImageView(Context context) {
        super(context);
        init();
    }

    public OverlappingImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        getWidthAndHeight();
        paint = new Paint();
        paint.setAlpha(0);
        /*在已有的图像上绘图将会在其上面添加一层新的图层，如果新图层的paint是不透明的,那么它将遮挡住下面的paint；
        如果新图层它是部分透明的,那么它不透明的地方将会被染上下面的颜色*/
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(0);
        //设置笔触和连接处能更加圆滑
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    @SuppressLint("DrawAllocation")
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //将图片绘制到frontCanvas画布上
        frontCanvas.drawBitmap(frontImage, null, rectF, null);
        //绘制背景广告
        canvas.drawBitmap(background, null, rectF, null);
        //绘制前景广告
        canvas.drawBitmap(frontBg, null, rectF, null);
        frontCanvas.drawCircle(width - offsetX, height - offsetY, r, paint);
    }

    public void setRecyclerView(ViewGroup parent) {
        if (!(parent instanceof RecyclerView))
            throw new RuntimeException("必须是android.support.v7.widget.RecyclerView");
        ((RecyclerView) parent).addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                getLocation();
            }
        });

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /*获取view的高宽*/
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        rectF = new RectF(0, 0, width, height);
        /*解析图片*/
        background = ((BitmapDrawable) getResources().getDrawable(images[0])).getBitmap();
        frontImage = ((BitmapDrawable) getResources().getDrawable(images[1])).getBitmap();
        /*创建一个空bitmap*/
        frontBg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //在frontBg 上做画
        frontCanvas = new Canvas(frontBg);
    }

    private void getWidthAndHeight() {
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        screenHeight = dm.heightPixels;
    }


    private void getLocation() {
        int[] location = new int[2];
        //获取view在屏幕上的坐标
        this.getLocationOnScreen(location);
        //int x = location[0];
        int y = location[1];
        //图片在屏幕中的话 0 < y  图片完全显示在屏幕中
        int height = y + getHeight();//view底部距离屏幕顶部的高度
        if (y > 0 && screenHeight >= height) {
            //图片显示在屏幕中了
            r = (float) ((screenHeight - height) * 1.5);
            frontCanvas.drawCircle(width - offsetX, this.height - offsetY, r, paint);
        } else {
            //向下滑动
            if (r < width)
                r = 0;
        }
        invalidate();
    }

    /**
     * 设置前后背景广告图片资源
     *
     * @param images [0] 背景图片
     *               [1] 前景图片
     */
    public void setImages(int[] images) {
        background = ((BitmapDrawable) getResources().getDrawable(images[0])).getBitmap();
        frontImage = ((BitmapDrawable) getResources().getDrawable(images[1])).getBitmap();
    }
}
